package com.jrx.anytxn.param.controller.investor;

import com.google.common.collect.Maps;
import com.jrx.anytxn.common.exception.TxnException;
import com.jrx.anytxn.i18n.response.TxnPage;
import com.jrx.anytxn.i18n.response.TxnRespResult;
import com.jrx.anytxn.param.dto.investor.PrInvestorTableReq;
import com.jrx.anytxn.param.dto.investor.PrInvestorTableRes;
import com.jrx.anytxn.param.service.investor.IPrInvestorTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import jrx.auth.filter.service.InfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.constraints.NotNull;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * 出资方控制器
 *
 * @author 黄秋平
 * @date 2020-05-12 14:11
 **/
@Api(tags = "出资方参数api接口")
@RestController
@RequestMapping(value = "/param/investor/investors")
public class PrInvestorTableController {

    private static final Logger logger= LoggerFactory.getLogger(PrInvestorTableController.class);


    @Resource
    private IPrInvestorTableService iPrInvestorTableService;

    @Resource
    private InfoService infoService;

    @ApiOperation(value = "根据租户ID和出资方ID查询出资方表信息", notes = "根据租户ID和出资方ID查询出资方表信息")
    @GetMapping(value = "/{tenantId}/{investorId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "tenantId",value = "租户ID",required = true),
            @ApiImplicitParam(name = "investorId",value = "出资方ID",required = true)
    })
    public TxnRespResult<PrInvestorTableRes> getByTenIdAndInvestorId( @PathVariable String tenantId,@NotNull @PathVariable String investorId) {
        TxnRespResult<PrInvestorTableRes> result = new TxnRespResult<>();
        PrInvestorTableRes res;
        try {
            res = iPrInvestorTableService.findByTenIdAndInvestorId(String.valueOf(infoService.getUser().getProjectId()),investorId );
                if (res.getFeeRate() != null && res.getFeeRate().compareTo(BigDecimal.valueOf(0)) != 0) {
                    res.setFeeRate(res.getFeeRate().multiply(BigDecimal.valueOf(100)));
                }
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "根据租户ID分页查询出资方表信息",notes = "根据租户ID分页查询出资方表信息")
    @GetMapping(value = "/pageNum/{pageNum}/pageSize/{pageSize}")
    @ApiImplicitParams({
            @ApiImplicitParam(name="pageNum",value="页码",required = true),
            @ApiImplicitParam(name="pageSize",value="页大小",required = true)})
    public TxnRespResult<TxnPage<PrInvestorTableRes>> getAllPageByTenId(@PathVariable(value = "pageNum") Integer pageNum, @PathVariable(value = "pageSize") Integer pageSize) throws TxnException {

        TxnRespResult<TxnPage<PrInvestorTableRes>> result = new TxnRespResult<>();
        TxnPage<PrInvestorTableRes> response;
        try {
            String tenantId = String.valueOf(infoService.getUser().getProjectId());
            response = iPrInvestorTableService.findAllPageByTenId(pageNum, pageSize, tenantId);
            result.getSuccess(response);
        }catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }

        return result;
    }

    @ApiOperation(value = "新增出资方表信息",notes = "新增出资方表信息")
    @PostMapping("/")
    public TxnRespResult<PrInvestorTableRes> createInvestorTable(@RequestBody PrInvestorTableReq prInvestorTableReq) {
        PrInvestorTableRes res;
        TxnRespResult<PrInvestorTableRes> result = new TxnRespResult<>();
        try {
            prInvestorTableReq.setTenantId(String.valueOf(infoService.getUser().getProjectId()));
            prInvestorTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (prInvestorTableReq.getFeeRate() != null && prInvestorTableReq.getFeeRate().compareTo(BigDecimal.ZERO) != 0) {
                prInvestorTableReq.setFeeRate(prInvestorTableReq.getFeeRate().divide(BigDecimal.valueOf(100)));
            }
            res = iPrInvestorTableService.addInvestorTable(prInvestorTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "编辑出资方表信息",notes = "编辑出资方表信息")
    @PutMapping("/")
    public TxnRespResult<PrInvestorTableRes> modifyInvestorTable(@RequestBody PrInvestorTableReq prInvestorTableReq) {
        TxnRespResult<PrInvestorTableRes> result = new TxnRespResult<>();
        PrInvestorTableRes res;
        try {
            prInvestorTableReq.setUpdateBy(infoService.getUser().getUserName());
            if (prInvestorTableReq.getFeeRate() != null && prInvestorTableReq.getFeeRate().compareTo(BigDecimal.ZERO) != 0) {
                prInvestorTableReq.setFeeRate(prInvestorTableReq.getFeeRate().divide(BigDecimal.valueOf(100)));
            }
            res = iPrInvestorTableService.modifyInvestorTable(prInvestorTableReq);
            result.getSuccess(res);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }

        return result;
    }

    @ApiOperation(value = "根据出资方ID和租户ID删除出资方表信息",notes = "根据出资方ID和租户ID删除出资方表信息")
    @DeleteMapping(value = "/{investorId}/{tenantId}")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "investorId",value = "出资方ID",required = true),
            @ApiImplicitParam(name = "tenantId",value = "租户ID",required = true)
    })
    public TxnRespResult<Boolean> removeByIdAndTenId(@NotNull @PathVariable String investorId,@PathVariable("tenantId") String tenantId) {
        TxnRespResult<Boolean> result = new TxnRespResult<>();
        Boolean flag;
        try {
            flag = iPrInvestorTableService.removeByInvestorIdAndTenId(investorId,String.valueOf(infoService.getUser().getProjectId()));
            result.getSuccess(flag);
        } catch (TxnException e) {
            logger.error(e.getMessage(),e);
            result.getFail(e.getErrCode(), e.getMessage());
        }
        return result;
    }

    @ApiOperation(value = "获得出资方id下拉信息", notes = "获得出资方id下拉信息")
    @GetMapping(value = "/list")
    public TxnRespResult<HashMap<String, List<PrInvestorTableRes>>> getInvestorInfoList() throws TxnException {
        TxnRespResult<HashMap<String, List<PrInvestorTableRes>>> result = new TxnRespResult<>();
        String tenantId=String.valueOf(infoService.getUser().getProjectId());
        List<PrInvestorTableRes> allPartner = iPrInvestorTableService.findAllByTenId(tenantId);
        HashMap<String, List<PrInvestorTableRes>> sysCodeMap = Maps.newHashMap();
        sysCodeMap.put("listData", allPartner);
        return result.getSuccess(sysCodeMap);
    }

}
